package morning.cat.demo1;

import org.apache.catalina.Context;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Bootstrap;
import org.apache.catalina.startup.Tomcat;

/**
 * @describe: Tomcat启动类
 * @author: morningcat.zhang
 * @date: 2019/2/22 1:40 AM
 */
public class TomcatDemo {
    public static void main(String[] args) throws LifecycleException {

        // 1.新建一个Tomcat对象
        Tomcat tomcat = new Tomcat();
        //TomcatWebServer server;

        Bootstrap bootstrap;


        // 2.创建 HTTP 连接器
        Connector connector = tomcat.getConnector();
        connector.setPort(8085);
        connector.setProtocol("HTTP/1.1");
        connector.setRedirectPort(8443);


        // 3.设置Host的属性
        Host host = tomcat.getHost();
        host.setName("localhost");
        host.setAppBase("webapps");

        // 4.加载class
        String classPath = System.getProperty("user.dir");
        Context context = tomcat.addContext(host, "/", classPath);

        if (context instanceof StandardContext) {
            StandardContext standardContext = (StandardContext) context;
            // 要给一个默认的web.xml文件（拷贝tomcat/conf目录下的web.xml作为默认的）
            standardContext.setDefaultContextXml("classpath:web2.xml");

            //把server设置进去
            Wrapper wrapper = tomcat.addServlet("/", "HelloServlet", new HelloServlet());
            wrapper.addMapping("/hello");
        }

        // 5.启动
        tomcat.start();
        // 6.强制Tomcat Server等待
        tomcat.getServer().await();

        // http://localhost:8085/hello
    }
}
